﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Xfm.Simple.Models;

namespace Xfm.Simple.Controllers
{

    public class FormsController : Controller
    {
        [AllowAnonymous]
        public IActionResult General()
        {

            return View();
        }
        [HttpPost]
        public IActionResult QuickExample(UserInfoVm model)
        {
            if (ModelState.IsValid)
            {
                return Content("验证通过");
            }
            return View(nameof(General), model);
        }
        //[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
        public IActionResult Advanced()
        {
            return View();
        }
        public IActionResult Editors()
        {
            var test = User;
            return View();
        }
        //[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
        public IActionResult Validation()
        {
            Console.WriteLine(User?.Identity?.Name);
            return View();
        }
        [AllowAnonymous]
        public async Task<IActionResult> Login()
        {
            var claimsPrincipal = new ClaimsPrincipal();

            //claimsIdentity.
            //ClaimsIdentity.
            var claims = new List<Claim>
                {
                    new Claim(ClaimTypes.NameIdentifier,"2",ClaimValueTypes.Integer),
                    new Claim("user", "userName"),
                    new Claim("user","User2"),
                    new Claim("role", "admin"),
                     new Claim("role", "admin2"),
                    new Claim(ClaimTypes.NameIdentifier,"1"),
                    new Claim(ClaimTypes.Role,"role2"),
                    new Claim(ClaimTypes.Role,"role3")
                };
            //new ClaimsPrincipal(new ClaimsIdentity("Cookie2"))

            var claimsIdentity = new ClaimsIdentity(claims, "CookieType", "user", "role");
            var claimsIdentity2 = new ClaimsIdentity(claims, "Password");
            //claimsIdentity.AddClaims(claims);
            //ClaimsIdentity.AddClaim(new Claim("identity"))
            claimsPrincipal.AddIdentity(claimsIdentity);
            claimsPrincipal.AddIdentity(claimsIdentity2);
            //var r= SignIn(claimsPrincipal, CookieAuthenticationDefaults.AuthenticationScheme);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal).ConfigureAwait(false);
            return RedirectToAction("Advanced");
        }
        public IActionResult Test()
        {
            return Ok(User);
        }
        public IActionResult Logout()
        {
            //SignOut(CookieAuthenticationDefaults.AuthenticationScheme);
            //await HttpContext.SignOutAsync().ConfigureAwait(false);
            return RedirectToAction("Validation");
        }
    }
}